package Listlist;
import java.util.Arrays;

public class MyArrayList implements Ilist{
    public int[] array;
    int usesize=0;
    public static final int DEFAULT_CAPACITY=10;

    @Override
    public void add(int date) {

        if(isFULL()){
            grow();
        }
        this.array[this.usesize]=date;
        this.usesize++;
    }
    private void checkPose(int pos) throws PosIllegal{
        if(pos<0||pos>usesize){
            throw new PosIllegal("Pos位置不合法！！！");
        }

    }

    @Override
    public void add(int pos, int date) {
        try{
            checkPose(pos);
        }catch (PosIllegal e){
            System.out.println("Pos位置不合法！");
        }
        if(isFULL()){
            grow();
        }
        for (int i = usesize-1; i >=pos ; i--) {
            this.array[i+1]=this.array[i];
        }

        this.array[pos]=date;
        this.usesize++;

    }

    @Override
    public void delete(int key) {

    }

    @Override
    public boolean contains(int pos) {
        for (int i = 0; i < this.usesize; i++) {
            if(this.array[i]==pos){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean isFULL() {
        return this.usesize==array.length;
    }

    @Override
    public int indexof(int pos) {
        for (int i = 0; i < this.usesize; i++) {
            if(this.array[i]==pos){
                return i;
            }
        }
        return 0;
    }

    @Override
    public int getvalue(int pos) {

        try{
            checkPose(pos);
            return array[pos];
        }catch (PosIllegal e){
            e.printStackTrace();
            return -1;
        }


    }

    @Override
    public void clear(int pos) {
        array[pos]=0;

    }

    @Override
    public int size() {
        return this.usesize;
    }

    @Override
    public void very(int lpos, int npos) {
        array[lpos]=npos;

    }

    @Override
    public void display() {
        for (int i = 0; i < this.usesize; i++) {
            System.out.print(this.array[i]+" ");
        }
    }

    public void grow(){
        this.array=Arrays.copyOf(array,2*this.array.length);
    }

    public MyArrayList(){
        this.array=new int [DEFAULT_CAPACITY];
    }
}
